angular.module('cash', [])
    .controller('cashCtrl', ['$scope', 'httpService', 'alertmsg', 'confirmservice', '$localStorage', '$location', function($scope, httpService, alertmsg, confirmservice, $localStorage, $location) {

        $scope.issublimt = false;
        $scope.current_type = 'bank';
        $scope.user = $localStorage.user;

        function updateData(type) {
            $scope.payData = {};
            if($scope.user) {
                angular.forEach($scope.user.deposit_accounts, function(v, k) {
                    if(v.channel_type == type) {
                        $scope.payData = v;
                    }
                });
            }
            $scope.payData.amount = parseFloat($scope.user.amount / 100, 10);
        }
        updateData($scope.current_type);
            

        $scope.issublimt = false;
        
        $scope.selectPayType = function(type) {
            $scope.issublimt = false;
            $scope.current_type = type;
            updateData(type);
        };

        // 提现
        $scope.cashMoney = function(bol, data) {
            if(!bol) {
                var money = angular.copy(data);
                money.amount = money.amount * 100;
                money.open_bank_code = data.open_bank;
                money.open_bank = data.channel_title;
                confirmservice('确认提现吗？').then(function(bool) {
                    if (bool) {
                        httpService.query('web/users/appoint_cash', money, 'post').then(function(res) {
                            if (res.code == 200) {
                                alertmsg('您的提现申请已提交，我们将在三个工作日内处理。');
                                window.history.go(-1);
                            } else {
                                alertmsg(res.msg);
                            }
                        });
                    }
                });
            } else {
                $scope.issublimt = true;
            }
                
        };

    }]);
